<?php


namespace app\home\controller;


use app\base\BaseController;
use app\home\severs\SaleContractDetailSever;
use PhpOffice\PhpSpreadsheet\IOFactory;
use think\Exception;
use think\facade\Db;
use think\facade\View;

class BuyIn extends BaseController
{
    protected $title='采购入库';
    protected $table='stock_order';
    protected $tableMx='stock_order_detail';
    protected $s_type=3;
    public function index()
    {
        $param=$this->request->param();
        if (request()->isAjax()) {
            $where[]=['so.status','>',-1];
            $where[]=['so.type','=',$this->s_type];

            if (isset($this->param['status'])&&$this->param['status']){
                $where[]=['so.status','=',$this->param['status']];
            }
            if (isset($this->param['name'])&&$this->param['name']){
                $where[]=['py.name|ad.name|so.orderNum','like','%'.$this->param['name'].'%'];
            }
            if (isset($this->param['orderDate'])&&$this->param['orderDate']){
                $dates=explode('~',$this->param['orderDate']);
                $where[]=['so.orderDate','between',$dates];
            }
            if (isset($param['saleUnitName'])&&$param['saleUnitName']){
                $where[]=['d.title','like','%'.$this->param['saleUnitName'].'%'];
            }
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $list = Db::name($this->table)
                ->alias('so')
                ->leftJoin('purveyor py','py.id=so.punterId')
                ->leftJoin('admin ad','ad.id=so.checkId')
                ->leftJoin('department d','d.id=so.saleUnitId')
                ->where($where)
                ->field('so.*,py.name as pName,ad.name as checkName,d.title as saleUnitName')
                ->order('so.id DESC')
                ->paginate($rows);
            return table_assign(0, '', $list);
        } else {
            return view();
        }
    }


    public function add()
    {
        $param = get_params();
        if (request()->isAjax()) {
            $param=$this->param;
            $mx=$param['mx']??[];
            unset($param['mx']);
            if (!$mx){
                return to_assign(1,'请添加明细');
            }
            if (!$param['punterId']){
                return to_assign(1,'参数有误');
            }
            $mxId=$mx['mxid']??[];
            $mdId=$mx['mdId']??[];
            $saleContractDetailId=$mx['saleContractDetailId']??[];
            $wId=$mx['wId']??[];
            $thisId=$mx['thisId']??[];
            $kcNum=$mx['kcNum']??[];
            $occupyNum=$mx['occupyNum']??[];
            $sodType=$mx['sodType']??[];
            $num=$mx['num']??[];
            $sodPrice=$mx['sodPrice']??[];
            $saleContractOrder=$mx['saleContractOrder']??[];
            $sodRemark=$mx['sodRemark']??[];
            $hsContract=$mx['hsContract']??[];
            $param['update_time']=time();
            $id=$this->param['id']??0;
            Db::startTrans();
            try {
                $param['orderNum']=dJBHBySql($this->table,'orderNum',getOrderPrefix($this->controller).date('Ymd'),false,1000);

                if ($id > 0) {
                    Db::name($this->table)->strict(false)->field(true)->update($param);
                    add_log('edit', $param['id'], $param,$this->title);
                } else {
                    $param['createId']=$this->uid;
                    $param['departId']=$this->did;
                    $param['create_time'] = time();
                    $param['type'] = $this->s_type;
                    $id = Db::name($this->table)->strict(false)->field(true)->insertGetId($param);
                    add_log('add', $id, $this->param,$this->title);
                }
                foreach ($mdId as $key=> $m){
                    $mxData=[
                        'orderId'=>$id,
                        'mdId'=>$mdId[$key],
                        'wId'=>$wId[$key],
                        'thisId'=>$thisId[$key],
                        'num'=>$num[$key],
                        'kcNum'=>$kcNum[$key]??0,
                        'occupyNum'=>$occupyNum[$key]??0,
                        'sodPrice'=>$sodPrice[$key],
                        'sodType'=>$sodType[$key]??2,
                        'saleContractDetailId'=>$saleContractDetailId[$key],
                        'saleContractOrder'=>$saleContractOrder[$key],
                        'hsContract'=>$hsContract[$key],
                        'sodRemark'=>$sodRemark[$key],
                        'type'=>$this->s_type,
                        'create_time'=>time(),
                        'update_time'=>time()
                    ];
                    if (isset($mxId[$key])&&$mxId[$key]){
                        unset($mxData['create_time']);
                        $mxData['id']=$mxId[$key];
                    }
                    Db::name($this->tableMx)->save($mxData);
                }

                Db::commit();
            }catch (\Exception $e){
                Db::rollback();
                return to_assign(1,'操作失败:'.$e->getMessage());
            }

            return to_assign();
        } else {
            $id = isset($param['id']) ? $param['id'] : 0;
            $detail=[];
            $mxList=[];
            if ($id > 0) {
                $detail = Db::name($this->table)->where('id', $id)->find();
                if ($detail['checkId']){
                    $saveInfo=Db::name('admin')
                        ->where('id',$detail['checkId'])
                        ->find();
                    $detail['checkName']=$saveInfo['name'];
                }

                $mxList=Db::name($this->tableMx)->alias('sod')
                    ->leftJoin('material_dossier md','md.id=sod.mdId')
                    ->leftJoin('sale_contract_detail scd','scd.id=sod.saleContractDetailId')
                    ->leftJoin('sale_contract xs','xs.orderNum=scd.saleContractOrderNum and xs.status>-1')
                    ->leftJoin('purveyor p','p.id=xs.punterId')
                    ->where('sod.status','>',-1)
                    ->where('sod.orderId',$id)
                    ->field('sod.*,md.name as mdName,md.buyCode,md.brand,md.specs,md.unit,md.bar_code,scd.saleContractOrderNum as xsHt,p.name as pName')
                    ->select();
            }else{
                $detail['orderNum']=dJBHBySql($this->table,'orderNum',getOrderPrefix($this->controller).date('Ymd'),false,1000);
            }
            $punterList=Db::name('purveyor')->where('status',1)
                ->where('dw_type',1)
                ->field('id,name')->select();
            view::assign('punterList',$punterList);
            $warehouse=Db::name('warehouse')->where('status','1')->field('id,name')->select();
            View::assign('warehouse', $warehouse);
            View::assign('detail', $detail);
            View::assign('mxList', $mxList);
            View::assign('id', $id);
            return view('form');
        }
    }

    public function saleContract()
    {
        $khId=$this->param['khId']??'';
        if (request()->isPost()) {
            if (!$khId){
                return table_assign(1,'请选择客户');
            }
            $where[]=['scd.status','=',4];
            $where[]=['scd.buyStatus','<>',3];
            $where[]=['sc.punterId','=',$khId];
            foreach (['buyCode','name','brand','specs'] as $v){
                if (isset($this->param[$v])&&$this->param[$v]){
                    $where[]=[$v,'like','%'.$this->param[$v].'%'];
                }
            }
            if (isset($this->param['cgHt'])&&$this->param['cgHt']){
                $where[]=['sc.orderNum','like','%'.$this->param['cgHt'].'%'];
            }
            $limit=$this->param['limit']??15;
            $list=Db::name('sale_contract_detail')
                ->alias('scd')
                ->leftJoin('sale_contract sc','sc.id=scd.orderId')
                ->leftJoin('material_dossier md','md.id=scd.mdId')
                ->leftJoin('sale_contract xs','xs.orderNum=scd.saleContractOrderNum and xs.status>-1')
                ->leftJoin('purveyor p','p.id=xs.punterId')
                ->field("scd.*,sc.orderNum,md.bar_code,md.name as mdName,md.brand,md.specs,md.buyCode,md.unit,p.name as pName")
                ->where($where)->paginate($limit)->each(function ($item){
                    $rukuNum=Db::name('stock_order_detail')
                        ->where('status',2)
                        ->where('type',3)
                        ->where('saleContractDetailId',$item['id'])->sum('num');
                    $unInNum=$item['bjNum']-$rukuNum;
                    $item['unInNum']=$unInNum<0?0:$unInNum;
                    $item['pName']=$item['pName']??'';
                    return $item;
                });
            return table_assign(0, '', $list);
        } else {
            view::assign('khId',$khId);
            return view('mx');
        }
    }

    public function removeMx()
    {
        $ids=$this->param['id']??[];
        if (!$ids){
            return to_assign(1,'参数缺失');
        }
        Db::startTrans();
        try {
            Db::name($this->tableMx)
                ->where('id',$ids)->update(['status'=>-1,'deleted'=>time()]);
            add_log('delete', $ids, $this->param,$this->title.'明细');
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            return to_assign(1,'操作失败:'.$e->getMessage());
        }

        return to_assign();
    }

    public function info($id)
    {

            $detail = Db::name($this->table)->where('id', $id)->find();
            if ($detail['checkId']){
                $saveInfo=Db::name('admin')
                    ->where('id',$detail['checkId'])
                    ->find();
                $detail['checkName']=$saveInfo['name'];
            }

        $mxList=Db::name($this->tableMx)->alias('sod')
            ->leftJoin('material_dossier md','md.id=sod.mdId')
            ->leftJoin('sale_contract_detail scd','scd.id=sod.saleContractDetailId')
            ->leftJoin('sale_contract xs','xs.orderNum=scd.saleContractOrderNum and xs.status>-1')
            ->leftJoin('purveyor p','p.id=xs.punterId')
            ->where('sod.status','>',-1)
            ->where('sod.orderId',$id)
            ->field('sod.*,md.name as mdName,md.buyCode,md.brand,md.specs,md.unit,md.bar_code,scd.saleContractOrderNum as xsHt,p.name as pName')
            ->select();
        $punterList=Db::name('purveyor')->where('status',1)
            ->where('dw_type',1)
            ->field('id,name')->select();
        view::assign('punterList',$punterList);
        $warehouse=Db::name('warehouse')->where('status','1')->field('id,name')->select();
        View::assign('warehouse', $warehouse);
        View::assign('detail', $detail);
        View::assign('mxList', $mxList);
        View::assign('id', $id);
        return view();
}
    public function delete()
    {
        $ids=$this->param['id']??[];
        if (!$ids){
            return to_assign(1,'参数缺失');
        }
        $status=Db::name($this->table)->where('id','in',$ids)->column('status');
        if (in_array('2',$status)){
            return to_assign(1,'已确认数据不能删除');
        }
        Db::startTrans();
        try {
            Db::name($this->table)->where('id','in',$ids)->update(['status'=>-1,'deleted'=>time()]);
            Db::name($this->tableMx)
                ->where('status','>',-1)
                ->where('orderId','in',$ids)->update(['status'=>-1,'deleted'=>time()]);
            add_log('delete', $ids, $this->param,$this->title);
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            return to_assign(1,'操作失败:'.$e->getMessage());
        }

        return to_assign();
    }

    public function onSure()
    {
        $id=$this->param['id']??0;
        if (!$id){
            return to_assign(1,'参数缺失');
        }
        $status=Db::name($this->table)->where('id',$id)->value('status');
        if ($status!=1){
            return to_assign(1,'参数有误');
        }
        $mx=Db::name($this->tableMx)->where('status',1)->where('orderId',$id)->count();
        if (!$mx){
            return to_assign(1,'请添加明细');
        }

        Db::startTrans();
        try {
            Db::name($this->table)->where('id',$id)->update([
                'checkId'=>$this->uid,
                'checkTime'=>date('Y-m-d H:i:s'),
                'status'=>2,
                'update_time'=>time()]);
            Db::name($this->tableMx)->where('status',1)
                ->where('orderId',$id)->update(['status'=>2,'update_time'=>time()]);
            $glList=Db::name($this->tableMx)->alias('sod')
                ->leftJoin('sale_contract_detail scd','scd.id= sod.saleContractDetailId')
                ->where('sod.sodType',2)
                ->where('sod.orderId',$id)
                ->where('sod.status','>',-1)
                ->field('sod.*,scd.saleContractDetailId as xshtMxId')
                ->select()->each(function ($item){
                    $item['xshtId']=Db::name('sale_contract_detail')->where('id',$item['xshtMxId'])
                        ->value('orderId');
                    $item['cghtId']=Db::name('sale_contract_detail')->where('id',$item['saleContractDetailId'])
                        ->value('orderId');
                    return $item;
                });
            $lockData=[];
            $xsMxIds=[];
            $cgMxId=[];
            foreach ($glList as $v){
                $lockData[]=[
                   'orderId'=>$v['xshtId'] ,
                   'detailId'=>$v['xshtMxId'] ,
                   'sType'=>1 ,
                   'mdId'=>$v['mdId'] ,
                   'wId'=>$v['wId'] ,
                   'stockOrderDetailId'=>$v['id'] ,
                   'buyContactId'=>$v['cghtId'] ,
                   'buyContactMxId'=>$v['saleContractDetailId'] ,
                   'lockNum'=>$v['num'] ,
                    'create_time'=>time(),
                    'update_time'=>time()
                ];
                $cgMxId[]=$v['saleContractDetailId'];
                $xsMxIds[]=$v['xshtMxId'];


            }
            if ($lockData){
                Db::name('sale_contract_lock')->insertAll($lockData);
            }
            add_log('onSure', $id, $this->param,$this->title);
            Db::commit();

        }catch (\Exception $e){
            Db::rollback();
            return to_assign(1,'操作失败:'.$e->getMessage());
        }
        foreach ($xsMxIds as $xs){
//            $num=Db::name('sale_contract_detail')->where('id',$xs)
//                ->value('bjNum');
//            $lockNum= Db::name('sale_contract_lock')->where('status','>',-1)
//                ->where('detailId',$xs)->sum('lockNum');
//            $updata=[
//                'update_time'=>time()
//            ];
//            if ($lockNum==0){
//                $updata['lockStatus']=1;
//            }elseif ($num<=$lockNum){
//                $updata['lockStatus']=3;
//            }elseif ($num>$lockNum){
//                $updata['lockStatus']=2;
//            }
//            Db::name('sale_contract_detail')->where('id',$xs)->update($updata);
            $bjNum=Db::name('sale_contract_detail')->where('id',$xs)->value('bjNum');
            $buyMxIds= Db::name('sale_contract_detail')
                ->where('status',4)
                ->where('type',2)
                ->where('saleContractDetailId',$xs)->column('id');
            $buyNum=Db::name($this->tableMx)->where('status','2')
                ->where('type',3)
                ->where('saleContractDetailId','in',$buyMxIds)->sum('num');
            if ($buyNum==0){
                Db::name('sale_contract_detail')->where('id',$xs)->update(['buyStatus'=>1,'update_time'=>time()]);
            }
            elseif ($bjNum>$buyNum){
                Db::name('sale_contract_detail')->where('id',$xs)->update(['buyStatus'=>2,'update_time'=>time()]);
            }elseif ($buyNum>=$bjNum){
                Db::name('sale_contract_detail')->where('id',$xs)->update(['buyStatus'=>3,'update_time'=>time()]);

            }
            foreach ($cgMxId as $ml){
                $bjNum=Db::name('sale_contract_detail')->where('id',$ml)->value('bjNum');
                $buyNum=Db::name($this->tableMx)
                    ->where('status','2')
                    ->where('type',3)
                    ->where('saleContractDetailId','in',$ml)->sum('num');
               if ($buyNum==0){
                   Db::name('sale_contract_detail')->where('id',$ml)->update(['buyStatus'=>1,'update_time'=>time()]);
               }
                elseif ($bjNum>$buyNum){
                    Db::name('sale_contract_detail')->where('id',$ml)->update(['buyStatus'=>2,'update_time'=>time()]);
                }elseif ($buyNum>=$bjNum){
                    Db::name('sale_contract_detail')->where('id',$ml)->update(['buyStatus'=>3,'update_time'=>time()]);

                }
            }
        }
        SaleContractDetailSever::changeLockStatus($xsMxIds);
        return to_assign(0,'操作成功');
    }

    public function onCancel()
    {
        $id=$this->param['id']??0;
        if (!$id){
            return to_assign(1,'参数缺失');
        }
        $status=Db::name($this->table)->where('id',$id)->value('status');
//        if ($status!=2){
//            return to_assign(1,'参数有误');
//        }
        $mx=Db::name($this->tableMx)->where('status','>',-1)->where('orderId',$id)->select();
        if (!$mx){
            return to_assign(1,'请添加明细');
        }
        $mxIds=Db::name($this->tableMx)->where('status','>',-1)->where('orderId',$id)->column('id');
        $hasRefound=Db::name($this->tableMx)->where('status','>',-1)
//            ->where('type',4)
            ->where('thisId','in',$mxIds)->count();
        if ($hasRefound){
            return to_assign(1,'已存在退货单或出库单，请删除后再试');
        }

        Db::startTrans();
        try {
            Db::name($this->table)->where('id',$id)->update([
                'checkId'=>$this->uid,
                'checkTime'=>date('Y-m-d H:i:s'),
                'status'=>1,
                'update_time'=>time()]);
            Db::name($this->tableMx)->where('status',2)
                ->where('orderId',$id)->update(['status'=>1,'refundStatus'=>1,'sodStatus'=>1,'update_time'=>time()]);
            $mxIds=Db::name($this->tableMx)->where('status','>',-1)->where('orderId',$id)->column('id');
           $xsMxIds= Db::name('sale_contract_lock')->where('stockOrderDetailId','in',$mxIds)
                ->where('status','>','-1')->column('detailId');
           $cgMxIds=Db::name($this->tableMx)->where('status','>',-1)->where('orderId',$id)
               ->column('saleContractDetailId');
            Db::name('sale_contract_lock')->where('stockOrderDetailId','in',$mxIds)
                ->where('status','>','-1')->update(['status'=>-1,'deleted'=>time()]);
//            Db::name('sale_contract_detail')
//                ->where('id','in',$xsMxIds)
//                ->where('status','>','-1')
//                ->update(['lockStatus'=>2,'update_time'=>time()]);

            add_log('back', $id, $this->param,$this->title);
            Db::commit();
            SaleContractDetailSever::changeLockStatus($xsMxIds);//更改于25-11-11 需要根据数量更改采购锁定状态
        }catch (\Exception $e){
            Db::rollback();
            return to_assign(1,'操作失败:'.$e->getMessage());
        }
//        halt($xsMxIds);//更改于25-10-09 入库单据 这里不需要反补采购状态
//        foreach ($xsMxIds as $xl){
//            $bjNum=Db::name('sale_contract_detail')->where('id',$xl)->value('bjNum');
//            $buyMxIds=Db::name('sale_contract_detail')
//                ->where('status','4')
//                ->where('type',2)
//                ->where('saleContractDetailId',$xl)->column('id');
//            $buyNum=Db::name($this->tableMx)
//                ->where('status','2')
//                ->where('type',3)
//                ->where('saleContractDetailId','in',$buyMxIds)->sum('num');
//            $refountNum=Db::name($this->tableMx)->where('status',2)
//                ->where('saleContractDetailId','in',$buyMxIds)
//                ->where('type',4)->sum('num');
//            $buyNum=$buyNum-$refountNum;
//            if ($buyNum==0){
//                Db::name('sale_contract_detail')->where('id',$xl)->update(['buyStatus'=>1,'update_time'=>time()]);
//            }
//            elseif ($bjNum>$buyNum){
//                Db::name('sale_contract_detail')->where('id',$xl)->update(['buyStatus'=>2,'update_time'=>time()]);
//            }elseif ($buyNum>=$bjNum){
//                Db::name('sale_contract_detail')->where('id',$xl)->update(['buyStatus'=>3,'update_time'=>time()]);
//
//            }
//           }

//        foreach ($cgMxIds as $cl){
////            $buyMxIds=Db::name('sale_contract_detail')
////                ->where('status','4')
////                ->where('type',4)
////                ->where('saleContractDetailId',$cl)->column('id');
//            $bjNum=Db::name('sale_contract_detail')->where('id',$cl)->value('bjNum');
//            $buyNum=Db::name($this->tableMx)
//                ->where('status','2')
//                ->where('type',3)
//                ->where('saleContractDetailId','=',$cl)->sum('num');
//            $refountNum=Db::name($this->tableMx)->where('status',2)
//                ->where('saleContractDetailId',$cl)
//                ->where('type',4)->sum('num');
//            $buyNum=$buyNum-$refountNum;
//            if ($buyNum==0){
//                Db::name('sale_contract_detail')->where('id',$cl)->update(['buyStatus'=>1,'update_time'=>time()]);
//            }
//            elseif ($bjNum>$buyNum){
//                Db::name('sale_contract_detail')->where('id',$cl)->update(['buyStatus'=>2,'update_time'=>time()]);
//            }elseif ($buyNum>=$bjNum){
//                Db::name('sale_contract_detail')->where('id',$cl)->update(['buyStatus'=>3,'update_time'=>time()]);
//
//            }
//        }
        return to_assign(0,'操作成功');
}
    public function importData()
    {
        Db::startTrans();
        try {
            if (!isset($_FILES['file'])){
                throw new Exception('请选择上传文件');
            }
            //限制上传表格类型
            $fileExtendName = substr(strrchr($_FILES['file']["name"], '.'), 1);
            //application/vnd.ms-excel  为xls文件类型
            if ($fileExtendName != 'xlsx') {
                throw new Exception('必须为excel表格，且必须为xlsx格式！');
            }

            if (is_uploaded_file($_FILES['file']['tmp_name'])) {
                // 有Xls和Xlsx格式两种
//            $objReader = \IOFactory::createReader('Xlsx');
                $objReader = IOFactory::createReader('Xlsx');

                $filename = $_FILES['file']['tmp_name'];

                $objPHPExcel = $objReader->setReadDataOnly(true)->load($filename);  //$filename可以是上传的表格，或者是指定的表格

                $sheet = $objPHPExcel->getSheet(0);   //excel中的第一张sheet
                $highestRow = $sheet->getHighestRow();       // 取得总行数
                $importData = [];
                $punterId=[];
                $wId=[];
                $num=[];
                $saleContractDetailId=[];

                //循环读取excel表格，整合成数组。如果是不指定key的二维，就用$data[i][j]表示。
                for ($j = 2; $j <= $highestRow; $j++) {
                    $punterId[]=(int)$objPHPExcel->getActiveSheet()->getCell("A" . $j)->getFormattedValue();
                    if ($punterId){
                        $wId[]=(int)$objPHPExcel->getActiveSheet()->getCell("B" . $j)->getFormattedValue();
                        $num[]=(float)$objPHPExcel->getActiveSheet()->getCell("C" . $j)->getFormattedValue();
                        $saleContractDetailId[]=(int)$objPHPExcel->getActiveSheet()->getCell("D" . $j)->getFormattedValue();
                    }
                }
                $res=$this->createImportData($punterId,$wId,$num,$saleContractDetailId);
                if ($res['code']!=0){
                    throw new Exception($res['msg']);
                }
                $zhuData=$res['data']['orderData'];
                $mxData=$res['data']['mxData'];
                if ($zhuData){
                    $id= Db::name($this->table)->insertGetId($zhuData);
                    foreach ($mxData as &$v){
                        $v['orderId']=$id;
                    }
                    Db::name($this->tableMx)->insertAll($mxData);
                    add_log('import', $id,$res['data'] ,$this->title);
                }
                Db::commit();
            }

        }catch (\Exception $e){
            Db::rollback();
            add_log('import', '', $e->getMessage(),$this->title);
            return to_assign(1,'操作失败:'.$e->getMessage());
        }
        return to_assign();

    }

    public function createImportData($punterId,$wId,$num,$saleContractDetailId)
    {
        $punterId=array_unique($punterId);
        if (count($punterId)!=1){
            return ['code'=>1,'msg'=>'参数不一致'];
        }
        $punterId1=Db::name('purveyor')->where('status',1)->where('id',$punterId[0])->value('id');

        if (!$punterId1){
            return  ['code'=>1,'msg'=>'参数有误'];
        }
        $zuData=[
            'orderNum'=>dJBHBySql($this->table,'orderNum',getOrderPrefix($this->controller).date('Ymd'),false,1000),
            'punterId'=>$punterId1,
            'orderDate'=>date('Y-m-d'),
            'type'=>$this->s_type,
            'createId'=>$this->uid,
            'departId'=>$this->did,
            'create_time'=>time(),
            'update_time'=>time()
        ];

        $mxData=[];
        foreach ($saleContractDetailId as $k=>$v){
            if (empty($wId[$k])||empty($num[$k])||empty($saleContractDetailId[$k])){
                return ['code'=>1,'msg'=>$k+1 .'行参数不能为空'];
            }
            $where[]=['scd.status','=',4];
            $where[]=['sc.punterId','=',$punterId1];
            $where[]=['scd.id','=',$saleContractDetailId[$k]];
            $info=$list=Db::name('sale_contract_detail')
                ->alias('scd')
                ->leftJoin('sale_contract sc','sc.id=scd.orderId')
                ->leftJoin('material_dossier md','md.id=scd.mdId')
                ->field("scd.*,sc.orderNum,md.bar_code,md.name as mdName,md.brand,md.specs,md.buyCode,md.unit")
                ->where($where)->find();
            if(!$info){
                return    ['code'=>1,'msg'=>$k+1 .'参数有误'];
            }

            $mxData[]=[
                'mdId'=>$info['mdId'],
                'wId'=>$wId[$k],
                'num'=>$num[$k],
                'thisId'=>'',
                'kcNum'=>0,
                'occupyNum'=>0,
                'saleContractOrder'=>$info['orderNum'],
                'saleContractDetailId'=>$saleContractDetailId[$k],
                'type'=>$this->s_type,
                'create_time'=>time(),
                'update_time'=>time()
            ];

        }
        return ['code'=>0,'msg'=>'操作成功','data'=>['orderData'=>$zuData,'mxData'=>$mxData]];
    }

}